//
//  ChatLeftCell.m
//  YouinRTCDemo
//
//  Created by 潘欣 on 2023/7/2.
//

#import "ChatLeftCell.h"

@implementation ChatLeftCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
       
        
        
        self.avatar = [UIImageView new];
        self.avatar.layer.cornerRadius = 20;
        self.avatar.layer.masksToBounds = YES;
        
        
        
        self.nameLabel = [UILabel new];
        self.nameLabel.font = [UIFont systemFontOfSize:12];
        
        self.time = [UILabel new];
        self.time.font = [UIFont systemFontOfSize:12];
        self.time.textColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        self.time.textAlignment = NSTextAlignmentRight;
        
        self.content = [UIView new];
        self.content.layer.cornerRadius = 3;
        self.content.backgroundColor = [UIColor whiteColor];
        
        self.text = [UILabel new];
        self.text.numberOfLines = 0;
        self.text.font = [UIFont systemFontOfSize:14];
        
        
    
       
        [self.contentView addSubview:self.avatar];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.time];
        
        [self.contentView addSubview:self.content];
        [self.content addSubview:self.text];
      
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.left.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).offset(5);
    }];
        
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.avatar.mas_right).offset(10);
        make.top.mas_equalTo(self.avatar);
        
    }];
    
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).offset(-10);
        make.top.mas_equalTo(self.nameLabel);
    }];
    
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(7);
        make.left.mas_equalTo(self.avatar.mas_right).offset(10);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
    }];
    
    [self.text mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.content).offset(10);
        make.bottom.mas_equalTo(self.content).offset(-10);
        make.left.mas_equalTo(self.content).offset(10);
        make.right.mas_equalTo(self.content).offset(-10);
        make.width.lessThanOrEqualTo(self.contentView.mas_width).offset(-92);
    }];
    
    
    
    
    [self layoutIfNeeded];
    
    //[self.content addCorner:UIRectCornerTopRight|UIRectCornerBottomLeft|UIViewContentModeBottomRight radius:5];

}

-(void)setMessage:(PrivateChatListModel *)message{
  
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:message.send_image]];
    self.nameLabel.text = message.send_nickname;
    if(![Tool strIsNULL:message.createDate]){
        NSString *timeStr = [message.createDate substringFromIndex:5];
        self.time.text = timeStr;
    }
    NSString *text = [message.content stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    self.text.text = text;
    [self layoutIfNeeded];
}
@end
